/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.redis;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Component
public class WebrtcGroupRedisTemplate extends StringRedisTemplate {

	private static final String KEY_REJECTED = "CALL_ROOM_%s_%d_REJECTED";

	private static final String KEY_QUITED = "CALL_ROOM_%s_%d_QUITED";

	private static final String KEY_BUSY = "CALL_ROOM_%s_%d_BUSY";

	private final Duration duration = Duration.ofHours(2);

	public WebrtcGroupRedisTemplate(RedisConnectionFactory connectionFactory) {
		super(connectionFactory);
	}

	public boolean isRejected(String tag,long uid) {
		return BooleanUtils.isTrue(super.hasKey(String.format(KEY_REJECTED,tag,uid)));
	}

	public boolean isQuited(String tag,long uid) {
		return BooleanUtils.isTrue(super.hasKey(String.format(KEY_QUITED,tag,uid)));
	}

	public boolean isBusy(String tag,long uid) {
		return BooleanUtils.isTrue(super.hasKey(String.format(KEY_QUITED,tag,uid)));
	}
	
	public void markRejected(String tag,long uid) {
		BoundValueOperations<String,String> operations = super.boundValueOps(String.format(KEY_REJECTED,tag,uid));
		operations.set("");
		operations.expire(duration);
	}

	public void markQuited(String tag,long uid) {
		BoundValueOperations<String,String> operations = super.boundValueOps(String.format(KEY_QUITED,tag,uid));
		operations.set("");
		operations.expire(duration);
	}

	public void markBusy(String tag,long uid) {
		BoundValueOperations<String,String> operations = super.boundValueOps(String.format(KEY_BUSY,tag,uid));
		operations.set("");
		operations.expire(duration);
	}

	public void cleanBusy(String tag,long uid) {
		this.delete(String.format(KEY_BUSY,tag,uid));
	}

	public void cleanRejected(String tag,long uid) {
		this.delete(String.format(KEY_REJECTED,tag,uid));
	}

	public void cleanQuited(String tag,long uid) {
		this.delete(String.format(KEY_QUITED,tag,uid));
	}

	public void cleanAll(String tag,long uid) {
		this.delete(String.format(KEY_REJECTED,tag,uid));
		this.delete(String.format(KEY_BUSY,tag,uid));
		this.delete(String.format(KEY_QUITED,tag,uid));

	}

}
